package cn.wit.luohui.day09;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class DemoKeywordsCount {
private static final String CONTENT="abstract 	continue	for	new	switch\r\n" + 
		"assert***	default	goto*	package	synchronized\r\n" + 
		"boolean	do	if	private	this\r\n" + 
		"break	double	implements	protected	throw\r\n" + 
		"byte	else	import	public	throws\r\n" + 
		"case	enum****	instanceof	return	transient\r\n" + 
		"catch	extends	int	short	try\r\n" + 
		"char	final	interface	static	void\r\n" + 
		"class	finally	long	strictfp**	volatile\r\n" + 
		"const*	float	native	super	while\r\n";

//α����
@Test
public void count() throws IOException {
	String file="D:\\暑期作业与笔记\\camp\\code\\day10\\src\\cn\\wit\\test\\DemoFile.java";

	List<String>lines=Files.readAllLines(Paths.get(file));
	System.out.println(1);
	
	//java���еĹؼ���
	Set<String>keywords=getKeywords();
	
//	// key: �ؼ��֣� value������ͳ��
	Map<String, Integer> counts = new HashMap<>();
	
	//����������ĵ��ʽ�������
	for(String line:lines) {
		String[] words=line.split("[\t.?;()<>]");// \t��ʾ�Ʊ��
		System.out.println(Arrays.toString(words));
		// ����ÿһ������
		// ����ǹؼ��֣��ͷ���һ��Map���ϣ�������һ
		for(String word:words) {
			if(keywords.contains(word)) {//
				// �����map�������ˣ���+1
				// û�У����ȷŽ�ȥ����������Ϊ1
				if(counts.containsKey(word)) {//�����map������
					Integer count=counts.get(word);
					counts.put(word, ++count);
				}else {
					counts.put(word, 1);
				}
			}
		}
	}
	//����map���ϣ���ӡ�ؼ��ֵ�ͳ����Ϣ
	for(Entry<String,Integer>entry:counts.entrySet()) {
		System.out.printf("%s \t %d \n", entry.getKey(), entry.getValue());
	}
}
@Test
//��ֹؼ���
public void test() {
	String[] split=CONTENT.split("[\t*]");
	for(int i=0;i<split.length;i++) {
		System.out.println(i);
		System.out.println(split[i]);
	}
}

public static Set<String> getKeywords() {
	Set<String>result=new HashSet<>();//����set���ϣ��Զ�ȥ��
	String[] split=CONTENT.split("[\t*]");
	for (int i=0;i<split.length;i++) {
		if("".equals(split[i])==false) {//���ǿյ��ַ�����ȥ��
			result.add(split[i]);
		}
	}
	return result;
}
}
